package action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import basic.PageInfo;
import basic.QuerySet;
import basic.ResultSet;
import basic.SortInfo;
import common.BaseAction;
import common.dict.CommonDict;
import entities.TReplace;
import interfaces.IReplaceService;
import util.ParameterUtil;
import utils.DateUtil;
import utils.ExcelUtil;
import utils.StringUtil;

public class ReplaceAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7507534255609348887L;

	private IReplaceService replaceService;

	private TReplace replaceBean;
	private List<TReplace> replaceList;

	private String repNo;
	private String repNos;
	private String bnkNo;
	private String repAddress;
	private String repCatalog;
	private Double repPrice;
	private String repState;
	private Date repDate;
	private Date repDateTop;
	private Date repDateEnd;
	private String repRemark;

	// 展示对象
	public String show() throws Exception {
		if (!replaceService.exist(repNo)) {
			addActionError("数据不存在");
			return INFO;
		} else {
			replaceBean = replaceService.fetch(repNo);
			return SUCCESS;
		}
	}

	// 编辑对象
	public String edit() throws Exception {
		if (!replaceService.exist(repNo)) {
			replaceBean = new TReplace();
		} else {
			replaceBean = replaceService.fetch(repNo);
		}
		return SUCCESS;
	}
	
	// 批量删除
	public String move() throws Exception {
		for (String repNo : StringUtil.split(repNos)) {
			replaceService.remove(repNo);
		}
		return SUCCESS;
	}

	// 持久化对象
	public String save() throws Exception {
		if (!replaceService.exist(repNo)) {
			replaceBean = new TReplace();
		} else {
			replaceBean = replaceService.fetch(repNo);
		}
		replaceBean.setRepNo(repNo);
		replaceBean.setRepAddress(repAddress);
		replaceBean.setRepCatalog(repCatalog);
		replaceBean.setRepPrice(repPrice);
		replaceBean.setRepState(repState);
		replaceBean.setRepDate(repDate);
		replaceBean.setRepRemark(repRemark);

		replaceService.persist(replaceBean);
		return SUCCESS;
	}

	// 编辑对象
	public String editreplace() throws Exception {
		return SUCCESS;
	}

	// 持久化对象
	public String savereplace() throws Exception {
		if (StringUtil.isEmpty(text)) {
			addActionError("上传列表不能为空");
			return INFO;
		}
		String[] contents = text.split("\r\n");
		if (contents.length == 0) {
			addActionError("上传的数据不符合格式");
			return INFO;
		}
		int step = 0;
		for (int i = 0; i < contents.length; i++) {
			String line = contents[i];
			try {
				String[] data = line.split("\\|");
				replaceBean=new TReplace();
				replaceBean.setRepNo(StringUtil.guid());
				replaceBean.setBnkNo(data[0].trim());
				replaceBean.setRepAddress(data[1].trim());
				replaceBean.setRepCatalog(data[2].trim());
				replaceBean.setRepPrice(new Double(data[3].trim()));
				replaceBean.setRepState("1");
				replaceBean.setRepDate(new Date());
				replaceBean.setRepRemark("");
				replaceService.persist(replaceBean);
			} catch (Exception e) {
				e.printStackTrace();
				addActionError("第" + (i + 1) + "行数据格式错误");
				continue;
			}

			addActionError("第" + (i + 1) + "行成功导入");
			step = step + 1;
		}
		addActionError("共成功 " + step + " 条数据");
		return SUCCESS;
	}

	// 列表展示
	// paramMap为查询条件,sortInfo为排序方式,pageInfo为分页对象
	// paramMap可为空,sortInfo可为空,pageInfo不能为空,如果需要查询全部则设置pageIndex为1,pageSize为Integer.MAX_VALUE
	public String list() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("repNo", StringUtil.split(repNo));
		paramMap.put("bnkNo", bnkNo);
		paramMap.put("repAddress", repAddress);
		paramMap.put("repCatalog", repCatalog);
		paramMap.put("repPrice", repPrice);
		paramMap.put("repState", repState);
		paramMap.put("repDate", ParameterUtil.dateCombin(repDateTop, repDateEnd));
		paramMap.put("repRemark", repRemark);

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "repDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(pageIndex, pageSize));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TReplace> resultSet = replaceService.search(querySet);
		replaceList = resultSet.getResultList();
		pageInfo = resultSet.getPageInfo();
		return SUCCESS;
	}

	// 导出数据 查询条件等同于列表查询条件
	public String export() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("repNo", StringUtil.split(repNo));
		paramMap.put("bnkNo", bnkNo);
		paramMap.put("repAddress", repAddress);
		paramMap.put("repCatalog", repCatalog);
		paramMap.put("repPrice", repPrice);
		paramMap.put("repState", repState);
		paramMap.put("repDate", ParameterUtil.dateCombin(repDateTop, repDateEnd));
		paramMap.put("repRemark", repRemark);

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "repDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(1, CommonDict.EXPORT_LIMIT));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TReplace> resultSet = replaceService.search(querySet);
		replaceList = resultSet.getResultList();

		List<String> fieldHead = new ArrayList<String>();
		fieldHead.add("编号");
		fieldHead.add("所属通道");
		fieldHead.add("伪站地址");
		fieldHead.add("产品类型");
		fieldHead.add("价格范围");
		fieldHead.add("是否可用");
		fieldHead.add("创建日期日期");
		fieldHead.add("创建日期时间");
		fieldHead.add("禁用原因");

		List<List<String>> fieldData = new ArrayList<List<String>>();

		for (TReplace replaceBean : replaceList) {
			List<String> fieldItem = new ArrayList<String>();
			fieldItem.add(replaceBean.getRepNo());
			fieldItem.add(replaceBean.getBnkNo());
			fieldItem.add(replaceBean.getRepAddress());
			fieldItem.add(replaceBean.getRepCatalog());
			fieldItem.add(replaceBean.getRepPrice().toString());
			fieldItem.add((replaceBean.getRepState().equals("0")) ? "无效" : "有效");
			fieldItem.add(DateUtil.format(replaceBean.getRepDate(), DateUtil.DATE));
			fieldItem.add(DateUtil.format(replaceBean.getRepDate(), DateUtil.TIME));
			fieldItem.add(replaceBean.getRepRemark());

			fieldData.add(fieldItem);
		}

		inputStream = ExcelUtil.excel(fieldHead, fieldData);
		return SUCCESS;
	}

	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////

	public IReplaceService getReplaceService() {
		return replaceService;
	}

	public void setReplaceService(IReplaceService replaceService) {
		this.replaceService = replaceService;
	}

	public TReplace getReplaceBean() {
		return replaceBean;
	}

	public void setReplaceBean(TReplace replaceBean) {
		this.replaceBean = replaceBean;
	}

	public List<TReplace> getReplaceList() {
		return replaceList;
	}

	public void setReplaceList(List<TReplace> replaceList) {
		this.replaceList = replaceList;
	}

	public String getRepNo() {
		return repNo;
	}

	public void setRepNo(String repNo) {
		this.repNo = repNo;
	}

	public String getRepNos() {
		return repNos;
	}

	public void setRepNos(String repNos) {
		this.repNos = repNos;
	}

	public String getRepAddress() {
		return repAddress;
	}

	public void setRepAddress(String repAddress) {
		this.repAddress = repAddress;
	}

	public String getRepCatalog() {
		return repCatalog;
	}

	public void setRepCatalog(String repCatalog) {
		this.repCatalog = repCatalog;
	}

	public Double getRepPrice() {
		return repPrice;
	}

	public void setRepPrice(Double repPrice) {
		this.repPrice = repPrice;
	}

	public String getRepState() {
		return repState;
	}

	public void setRepState(String repState) {
		this.repState = repState;
	}

	public Date getRepDate() {
		return repDate;
	}

	public void setRepDate(Date repDate) {
		this.repDate = repDate;
	}

	public Date getRepDateTop() {
		return repDateTop;
	}

	public void setRepDateTop(Date repDateTop) {
		this.repDateTop = repDateTop;
	}

	public Date getRepDateEnd() {
		return repDateEnd;
	}

	public void setRepDateEnd(Date repDateEnd) {
		this.repDateEnd = repDateEnd;
	}

	public String getRepRemark() {
		return repRemark;
	}

	public void setRepRemark(String repRemark) {
		this.repRemark = repRemark;
	}

	public String getBnkNo() {
		return bnkNo;
	}

	public void setBnkNo(String bnkNo) {
		this.bnkNo = bnkNo;
	}

	
	
}
